package com.student.recruitment.app.dao;

import java.util.List;



import org.hibernate.Criteria;
import org.hibernate.Query;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Repository;

import com.student.recruitment.app.entity.EmployeeEntity;

@Repository
public class EmployeeDaoImpl implements EmployeeDAO
{
    @Autowired
    private SessionFactory sessionFactory;
    
    @Override
    public void addEmployee(EmployeeEntity employee) 
    {
        int a=employee.getFirst()*2+employee.getSecond()+employee.getThird()*3;
        employee.setSum(a);
        this.sessionFactory.getCurrentSession().saveOrUpdate(employee);
    }
    @SuppressWarnings("unchecked")
    @Override
    public List<EmployeeEntity> getAllEmployees() 
    {
    	return this.sessionFactory.getCurrentSession().createQuery("from EmployeeEntity").list();
    }
    @Override
    public void deleteEmployee(Integer employeeId) 
    {
        EmployeeEntity employee = (EmployeeEntity) sessionFactory.getCurrentSession().load(
                EmployeeEntity.class, employeeId);
        if (null != employee) {
            this.sessionFactory.getCurrentSession().delete(employee);
        }
    }
    @Override
    public void editEmployee(EmployeeEntity employee) 
    { 
        if (null != employee) 
        {
            this.sessionFactory.getCurrentSession().update(employee);   
        }
    }
    @Override
    public EmployeeEntity getEmployee(Integer employeeId) 
    {
    	return (EmployeeEntity)sessionFactory.getCurrentSession().get(EmployeeEntity.class, employeeId);
    }
    @SuppressWarnings("unchecked")
	@Override
	public List<EmployeeEntity> getMyCriteriaRecords(String kryteria, String nazwa) 
	{
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(EmployeeEntity.class); 
		criteria.add(Restrictions.eq(kryteria, nazwa));
		return criteria.list();
	}
	@Override
	public String getEmployeeStatus(String username) 
	{
		Query query = sessionFactory.getCurrentSession().createQuery("from EmployeeEntity where username = :username");
		query.setParameter("username", username);
    	List<EmployeeEntity> list = query.list();
    	EmployeeEntity student = (EmployeeEntity)list.get(0);
    	String status = student.getStatus();
		return status;
	}
	@Override
	public void acceptEmployee(Integer employeeId) 
	{
		EmployeeEntity employee = (EmployeeEntity) sessionFactory.getCurrentSession().load(
                EmployeeEntity.class, employeeId);
        if (null != employee) 
        {
            employee.setStatus("przyjety");
        }
	}
	@Override
	public List<EmployeeEntity> SortResultsDirectionOfAmount(String direction, int ilosc) 
	{	
		Query query1 = sessionFactory.getCurrentSession().createQuery("from EmployeeEntity e where e.direction = ?  order by e.sum desc");
		query1.setString(0, direction );
		Object x = (int) ilosc;
		List<EmployeeEntity> list = query1.list().subList(0, ilosc);
		return list;
	}
	@Override
	public List<EmployeeEntity> SortResultsDirectionOfPoints(String direction, int points) 
	{
		Query query = sessionFactory.getCurrentSession().createQuery("from EmployeeEntity e where e.direction = ? and e.sum > ? order by e.sum desc");
		query.setString(0, direction );
		query.setParameter(1, points);
		List<EmployeeEntity> list = query.list();
		return list;
	}
	@Override
	public EmployeeEntity getEmployeeData(String username) 
	{
		Query query = sessionFactory.getCurrentSession().createQuery("from EmployeeEntity where username = :username");
		query.setParameter("username", username);
    	List<EmployeeEntity> list = query.list();
    	EmployeeEntity student = (EmployeeEntity)list.get(0);
		return student;
	}
	@Override
	public int getEmployeePoints(String username) 
	{
		Query query = sessionFactory.getCurrentSession().createQuery("from EmployeeEntity where username = :username");
		query.setParameter("username", username);
    	List<EmployeeEntity> list = query.list();
    	EmployeeEntity student = (EmployeeEntity)list.get(0);
    	int punkty = student.getSum();
		return punkty;
	}
}